/*
 * $Id: PolisUserTestCase.java 27 2007-04-13 01:02:48Z aaron.day $
 */
package org.crazydays.core.data;


import java.util.Set;
import java.util.Iterator;

import org.hibernate.Session;
import org.hibernate.HibernateException;

import junit.framework.TestCase;

import org.crazydays.hibernate.HibernateUtil;

/**
 * PolisUserTestCase
 */
public class PolisUserTestCase
	extends TestCase
{
	/**
	 * Test equals method.
	 */
	public void testEquals()
	{
		PolisUser guest1 = (PolisUser) HibernateUtil
				.loadPolisDataByName(PolisUser.class,
						PolisGroup.NAME_GUEST);
		assertNotNull("guest1 == null", guest1);
		PolisUser guest2 = (PolisUser) HibernateUtil
				.loadPolisDataByName(PolisUser.class,
						PolisGroup.NAME_GUEST);
		assertNotNull("guest2 == null", guest2);

		PolisUser admin1 = (PolisUser) HibernateUtil
				.loadPolisDataByName(PolisUser.class,
						PolisGroup.NAME_ADMIN);
		assertNotNull("admin1 == null", admin1);
		PolisUser admin2 = (PolisUser) HibernateUtil
				.loadPolisDataByName(PolisUser.class,
						PolisGroup.NAME_ADMIN);
		assertNotNull("admin2 == null", admin2);

		assertTrue("guest1 != guest2", guest1.equals(guest2));
		assertTrue("guest2 != guest1", guest2.equals(guest1));
		assertTrue("guest1 != guest1", guest1.equals(guest1));

		assertTrue("admin1 != admin2", admin1.equals(admin2));
		assertTrue("admin2 != admin1", admin2.equals(admin1));
		assertTrue("admin1 != admin1", admin1.equals(admin1));

		assertFalse("admin1 == guest1", admin1.equals(guest1));
		assertFalse("guest1 == admin1", guest1.equals(admin1));
	}

	/**
	 * Test guest.
	 */
	public void testGuest()
	{
		Session session = HibernateUtil.getSession();
		try {
			PolisUser user = (PolisUser) HibernateUtil
					.loadPolisDataByName(session,
							PolisUser.class,
							PolisGroup.NAME_GUEST);

			assertNotNull("user == null", user);

			// TODO: check fields

			// test owned groups
			Set<PolisGroup> ownedGroups = user.getOwnedGroups();
			assertNotNull("ownedGroups == null", ownedGroups);
			assertEquals("ownedGroups.size != 0", ownedGroups
					.size(), 0);

			// check groups
			Set<PolisGroup> groups = user.getGroups();
			assertNotNull("groups == null", groups);
			assertEquals("groups.size != 1", groups.size(), 1);

			Iterator<PolisGroup> i = groups.iterator();
			while (i.hasNext()) {
				PolisGroup group = i.next();

				if (group.getName().equals(
						PolisGroup.NAME_GUEST)) {
					continue;
				}

				fail("Unknown group: " + group.getName());
			}

			// test owned acls
			Set<PolisAcl> ownedAcls = user.getOwnedAcls();
			assertNotNull("ownedAcls == null", ownedAcls);
			assertEquals("ownedAcls.size != 0", ownedAcls.size(), 0);
		} catch (HibernateException e) {
			fail("HibernateException: " + e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

	/**
	 * Test Admin.
	 */
	public void testAdmin()
	{
		Session session = HibernateUtil.getSession();
		try {
			// load user
			PolisUser user = (PolisUser) HibernateUtil
					.loadPolisDataByName(session,
							PolisUser.class,
							PolisGroup.NAME_ADMIN);

			assertNotNull("user == null", user);

			// TODO: check fields

			// test owned groups
			Set<PolisGroup> ownedGroups = user.getOwnedGroups();
			assertNotNull("ownedGroups == null", ownedGroups);
			assertEquals("ownedGroups.size != 3", ownedGroups
					.size(), 3);

			Iterator<PolisGroup> o = ownedGroups.iterator();
			while (o.hasNext()) {
				PolisGroup group = o.next();

				if (group.getName().equals(
						PolisGroup.NAME_ADMIN)) {
					continue;
				}
				if (group.getName().equals(
						PolisGroup.NAME_GUEST)) {
					continue;
				}
				if (group.getName()
						.equals(PolisGroup.NAME_USER)) {
					continue;
				}

				fail("Unknown group: " + group.getName());
			}

			// check groups
			Set<PolisGroup> groups = user.getGroups();
			assertNotNull("groups == null", groups);
			assertEquals("groups.size != 2", groups.size(), 2);

			Iterator<PolisGroup> i = groups.iterator();
			while (i.hasNext()) {
				PolisGroup group = i.next();

				if (group.getName().equals(
						PolisGroup.NAME_ADMIN)) {
					continue;
				}
				if (group.getName()
						.equals(PolisGroup.NAME_USER)) {
					continue;
				}

				fail("Unknown group: " + group.getName());
			}

			// test owned acls
			Set<PolisAcl> ownedAcls = user.getOwnedAcls();
			assertNotNull("ownedAcls == null", ownedAcls);
			assertEquals("ownedAcls.size != 3", ownedAcls.size(), 3);

			Iterator<PolisAcl> j = ownedAcls.iterator();
			while (j.hasNext()) {
				PolisAcl acl = j.next();

				if (acl.getName().equals(PolisAcl.NAME_ADMIN)) {
					continue;
				}
				if (acl.getName().equals(PolisAcl.NAME_GUEST)) {
					continue;
				}
				if (acl.getName().equals(PolisAcl.NAME_USER)) {
					continue;
				}

				fail("Unknown acl: " + acl.getName());
			}

		} catch (HibernateException e) {
			fail("HibernateException: " + e.getMessage());
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}
}
